import Vue from 'vue'
import VueRouter from 'vue-router'

// 引入5个组件
import HomeView from '@/views/HomeView/HomeView'
import CategoryView from '@/views/CategoryView/CategoryView'
import FindView from '@/views/FindView/FindView'
import CartView from '@/views/CartView/CartView'
import MineView from '@/views/MineView/MineView'

Vue.use(VueRouter)

const routes = [{
        path: '/',
        redirect: '/homeview'
    },
    {
        path: '/homeview',
        component: HomeView,
        // 路由的元信息:共同组件的显示和隐藏的切换信息,组件是否缓存,组件的title等相关信息
        meta: {
            // 隐藏tab栏是否
            hiddenTab: false,
            title: '首页'
        }
    },
    {
        path: '/categoryview',
        component: CategoryView,
        // 路由的元信息
        meta: {
            hiddenTab: false,
            title: '分类'
        }
    },
    {
        path: '/findview',
        component: FindView,
        // 路由的元信息
        meta: {
            hiddenTab: false,
            title: '发现'
        }
    },
    {
        path: '/cartview',
        component: CartView,
        // 路由的元信息
        meta: {
            hiddenTab: false,
            title: '购物车'
        }
    },
    {
        path: '/mineview',
        component: MineView,
        // 路由的元信息
        meta: {
            hiddenTab: false,
            title: '我的'
        }
    },
    {
        path: '/searchview',
        // 路由的元信息
        meta: {
            hiddenTab: false,
            title: '搜索'
        },
        // 路由的懒加载--首屏加载慢的问题--按需引入
        component: () =>
            import ('../views/SearchView/SearchView.vue')
    },
    {
        path: '/loginview',
        // 路由的元信息
        meta: {
            hiddenTab: true,
            title: '登录'
        },
        // 路由的懒加载--首屏加载慢的问题--按需引入
        component: () =>
            import ('../views/LoginView/LoginView.vue')
    },

]

const router = new VueRouter({
    routes
});

// 全局守卫-前置首卫
router.beforeEach((to, from, next) => {
    console.log(to.meta);
    if (to.meta.title != undefined && to.meta.title != null) {
        document.title = to.meta.title
    } else {
        document.title = '大商创商城系统'
    }
    // 放行
    next(true);
});


export default router